import requests
from lxml import etree
import DBUtil
import Sha1Util
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
def shoye():
    url='https://www.lianjia.com/city/'
    reon=requests.get(url=url,headers=headers)
    wangye=reon.text
    return wangye
def guangdong():
    wangye=shoye()
    tree = etree.HTML(wangye)
    guangzong = tree.xpath('/html/body/div[2]/div[2]/div/div/ul/li[5]/div[2]/div[1]/div/text()')
    gaungzong_qy=tree.xpath('/html/body/div[2]/div[2]/div/div/ul/li[5]/div[2]/div[1]/ul/li/a/text()')
    gaungzong_qy_url=tree.xpath('/html/body/div[2]/div[2]/div/div/ul/li[5]/div[2]/div[1]/ul/li/a/@href')
    esho_url=[]
    for i in gaungzong_qy_url:
        reon=requests.get(url=i,headers=headers)
        wangye=reon.text
        tree = etree.HTML(wangye)
        esho=tree.xpath('/html/body/div[1]/div/div[3]/div/ul/li[1]/a/@href')
        esho_url.append(esho[0])
    region_name_href_dict = dict(zip(gaungzong_qy, esho_url))
    print(region_name_href_dict)
    for i,l in region_name_href_dict.items():
        id = Sha1Util.jia_mi(i + l)
        sql = '''INSERT into `guangdong` (id,name,url) VALUES ('%s','%s','%s')
                                            ''' % (id, i, l)
        DBUtil.updateTable(sql)
guangdong()
